package com.njym.java.security.auth.service.impl;
import com.njym.java.security.api.vo.user.AppUserInfo;
import com.njym.java.security.api.vo.user.UserInfo;
import com.njym.java.security.auth.common.util.jwt.JWTInfo;
import com.njym.java.security.auth.feign.AUserService;
import com.njym.java.security.auth.feign.IUserService;
import com.njym.java.security.auth.service.AppAuthService;
import com.njym.java.security.auth.util.user.JwtAuthenticationRequest;
import com.njym.java.security.auth.util.user.JwtTokenUtil;
import com.njym.java.security.common.exception.auth.UserInvalidException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @program: family-benefit
 *
 * @description:
 *
 * @author: lijie
 *
 * @create: 2019-11-14 11:09
 **/
@Service
public class AppAuthServiceImpl implements AppAuthService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private AUserService userService;
    @Override
    public AppUserInfo applogin(JwtAuthenticationRequest authenticationRequest) throws Exception {
        AppUserInfo info = userService.validate(authenticationRequest);
        if (!StringUtils.isEmpty(info.getId())) {
          String  token =  jwtTokenUtil.generateToken(new JWTInfo(info.getMobile(), info.getId() + "", info.getName()));
            info.setToken(token);
            return info;
        }
        throw new UserInvalidException("用户不存在或账户密码错误!");
    }
}
